import axios from "axios";

export default {
  state: {
    curClues: [],
    curCluesLength: 0,
  },
  getters: {},
  mutations: {},
  actions: {
    addClue(context, data) {
      let url = "/clue/add";
      let postData = data.clue;
      postData.firstChoice = "1";
      postData.firstMajor = "1";
      postData.secondChoice = "1";
      postData.secondMajor = "1";
      postData.candidateType = "1";

      let config = {
        headers: {
          Authorization: "Bearer " + data.token,
        },
      };
      axios
        .post(url, postData, config)
        .then((resp) => data.success(resp))
        .catch((resp) => data.error(resp));
    },
    getClueByCondition(context, data) {
      let url = "/clue/get/clue/by/condition2";
      let postData = data.condition;
      let config = {
        headers: {
          Authorization: "Bearer " + data.token,
        },
      };
      axios
        .post(url, postData, config)
        .then((resp) => {
          context.state.curClues = resp.data.data;
          data.success(resp);
        })
        .catch((resp) => data.error(resp));
    },
    getClueByConditionNum(context, data) {
      let url = "/clue/get/clue/by/condition2/num";
      let postData = data.condition;
      let config = {
        headers: {
          Authorization: "Bearer " + data.token,
        },
      };
      axios
        .post(url, postData, config)
        .then((resp) => {
          context.state.curCluesLength = resp.data.data;
          data.success(resp);
        })
        .catch((resp) => data.error(resp));
    },
    deleteClueById(context, data) {
      let url = "/clue/delete/by/id";
      let postData = { id: data.id };
      let config = {
        headers: {
          Authorization: "Bearer " + data.token,
        },
      };
      axios
        .post(url, postData, config)
        .then((resp) => data.success(resp))
        .catch((resp) => data.error(resp));
    },
    updateClue(context, data) {
      let url = "/clue/update";
      let postData = data.clue;
      let config = {
        headers: {
          Authorization: "Bearer " + data.token,
        },
      };
      axios
        .post(url, postData, config)
        .then((resp) => data.success(resp))
        .catch((resp) => data.error(resp));
    },
    setClueReported(context, data) {
      let url = "/clue/set/reported";
      let postData = {
        id: data.id,
      };
      let config = {
        headers: {
          Authorization: "Bearer " + data.token,
        },
      };
      axios
        .post(url, postData, config)
        .then((resp) => {
          data.success(resp);
        })
        .catch((resp) => {
          data.error(resp);
        });
    },
    unsetClueReported(context, data) {
      let url = "/clue/unset/reported";
      let postData = {
        id: data.id,
      };
      let config = {
        headers: {
          Authorization: "Bearer " + data.token,
        },
      };
      axios
        .post(url, postData, config)
        .then((resp) => {
          data.success(resp);
        })
        .catch((resp) => {
          data.error(resp);
        });
    },
    statisticsByConditions(context, data) {
      let url = "/clue/statistics/by/conditions";
      let postData = data.condition;
      let config = {
        headers: {
          Authorization: "Bearer " + data.token,
        },
      };
      axios
        .post(url, postData, config)
        .then((resp) => {
          data.success(resp);
        })
        .catch((resp) => {
          data.error(resp);
        });
    },
    generateClueStatistic(context, data) {
      let url = "/clue/generate/clue/statistic";
      let postData = data.condition;
      let config = {
        headers: {
          Authorization: "Bearer " + data.token,
        },
      };
      axios
        .post(url, postData, config)
        .then((resp) => {
          data.success(resp);
        })
        .catch((resp) => {
          data.error(resp);
        });
    },
    generateClueExcel(context, data) {
      let url = "/clue/generate/excel/by/condition";
      let postData = data.condition;
      let config = {
        headers: {
          Authorization: "Bearer " + data.token,
        },
      };

      axios({
        method: "post",
        url: url,
        data: postData,
        responseType: "blob",
        headers: config.headers,
      })
        .then((resp) => {
          const url = window.URL.createObjectURL(new Blob([resp.data]));
          const link = document.createElement("a");
          link.href = url;
          link.setAttribute("download", "output.xlsx"); // 指定文件名，根据实际情况进行修改
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
          data.success(resp);
        })
        .catch((resp) => {
          data.error(resp);
        });
    },
  },
  modules: {},
};
